const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = function (isDev) {
  // console.log(path.resolve(__dirname, "../src"), "----");
  const thread = {
    loader: "thread-loader",
    options: {
      workers: require("os").cpus().length - 1, // ✅ 正确：workers 放在 options 中
    },
  };
  return {
    cache: {
      type: "filesystem", // 启用文件系统缓存
      cacheDirectory: path.resolve(__dirname, ".temp_cache"), // 缓存存放路径
      buildDependencies: {
        config: [__filename], // 当配置文件变更时，自动刷新缓存
      },
    },
    //入口、出口
    entry: path.resolve(__dirname, "../src/index.tsx"),
    output: {
      path: path.resolve(__dirname, "../dist"),
      filename: "static/js/[name].[fullhash:8].js",
      clean: true,
      publicPath: "/",
    },
    //resolvs 解析文件
    resolve: {
      extensions: [".tsx", ".jsx", ".less", ".ts", ".js", ".json"],
      modules: [path.resolve(__dirname, "src"), "node_modules"],
      alias: {
        "@": path.resolve(__dirname, "../src"),
        "@pages": path.resolve(__dirname, "../src/pages"),
        "@components": path.resolve(__dirname, "../src/pages/components"),
        "@api": path.resolve(__dirname, "../src/api"),
        "pdfjs-dist": path.resolve(__dirname, "../node_modules/pdfjs-dist"),
      },
    },
    module: {
      rules: [
        {
          test: /\.(tsx|ts|jsx|js)$/,
          use: [
            thread,
            {
              loader: "babel-loader",
              options: {
                exclude: /node_modules/,
                configFile: path.resolve(__dirname, "../.babelrc"),
              },
            },
          ],
        },
        {
          test: /\.less$/,
          use: [
            isDev ? "style-loader" : MiniCssExtractPlugin.loader,
            "css-loader",
            {
              loader: "postcss-loader",
              options: {
                postcssOptions: {
                  plugin: [require("autoprefixer")],
                },
              },
            },
            "less-loader",
          ],
        },
        {
          test: /\.css$/,
          use: [
            isDev ? "style-loader" : MiniCssExtractPlugin.loader,
            "css-loader",
            {
              loader: "postcss-loader",
              options: {
                postcssOptions: {
                  plugin: [require("autoprefixer")],
                },
              },
            },
          ],
        },
        {
          test: /pdf.worker.min\.js$/,
          use: { loader: "worker-loader" },
          type: "javascript/auto", // 用于加载 worker 文件
        },
        {
          test: /\.pdf$/,
          use: [
            {
              loader: "file-loader",
              options: {
                name: "[name].[hash].[ext]", // 生成的文件名和路径
                outputPath: "assets/pdf/", // PDF 文件输出的目录
              },
            },
          ],
        },
        {
          test: /\.(png|jpe?g|gif|svg)$/i, // 匹配图片文件
          type: "asset/resource", // 自动选择导出为 base64 或独立文件
          parser: {
            dataUrlCondition: {
              maxSize: 8 * 1024, // 小于 8kb 的图片将被转为 base64
            },
          },
          generator: {
            filename: "assets/[name][ext]", // 控制输出目录和文件名
          },
        },
        {
          test: /\.(mp4|wmv|rvmb)$/,
          generator: {
            filename: "static/media/[name].[contenthash:8].[exy]",
          },
        },
        {
          test: /\.(eot|ttf|otf)$/,
          generator: {
            filename: "static/fonts/[name].[contenthash:8].[exy]",
          },
        },
      ],
      //plugin 部分
    },
    plugins: [
      new HtmlWebpackPlugin({
        template: path.resolve(__dirname, "../public/index.html"),
      }),
      new MiniCssExtractPlugin({
        filename: isDev ? "static/css[name].css" : "static/css[name].[contenthash:8].css",
      }),
    ],
  };
};
